var private const	PrintScale_pos = 1;
var private	get_pos, center;
var private	scal[3];
function private PrintScale;
function private Input_scale;

function private Input_scale()
{
	var	mat;
	var	temp;

	DlogOpen( "攞棪擖椡", 5 );
	DlogString( 0, "倃攞棪", tostring( scal[0] ), 9 );
	DlogString( 2, "倄攞棪", tostring( scal[1] ), 9 );
	DlogString( 4, "倅攞棪", tostring( scal[2] ), 9 );
	if( DlogWait() )
	{
		scal[0] = atof( DlogAnswer( 0 ));
		scal[1] = atof( DlogAnswer( 2 ));
		scal[2] = atof( DlogAnswer( 4 ));
		mat = scale( UNIT_MATRIX, scal[0], scal[1], scal[2] );
		temp = Position( get_pos )- Position( center );
		cur = center + temp*mat;
		Cursor( cur );
		PrintScale();
	}
}

function private PrintScale()
{
	var	deg[3], i, temp, key;
	var	st[3], title;

	st = {"倃:", "倄:", "倅:" };
	title = "";
	for( i = 0;i<3;i++ )
	{
		temp=tostring( scal[i] );
		key = search( temp, "." );
		temp = substr( temp, key+3 );
		if( !temp < 6 )
			temp = substr( "     "+temp, -6 );
		title += " " + st[i] + temp;
	}
	Status_title[PrintScale_pos] = title;
	DrawStatus();
}

//
//	僩儕儉僔僼僩僉乕墴壓帪偺僇乕僜儖偺僩儕儈儞僌乮僗働乕儖梡乯
//
function private TrimCursorforScale( win_type )
{
	var	a[3], b[3], cur_vec[3], center_vec[3];
	var	i, j, k;

	element((Position( get_pos ) - Position(center)), a );
	element((Position( cur )- Position(center)), b );
	Position( cur, cur_vec );
	Position( center, center_vec );
	for( i = 0;i<3;i++ )
	{
		a[i] = 0.0 + a[i];
		b[i] = 0.0 + b[i];
	}
	if( win_type == WIN_XY )
	{
		i = 0;
		j = 1;
		k = 2;
	}
	else if( win_type == WIN_YZ )
	{
		i = 1;
		j = 2;
		k = 0;
	}
	else if( win_type == WIN_ZX )
	{
		i = 2;
		j = 0;
		k = 1;
	}
	else
		return;
	if( abs( a[i] ) < 0.00001)
	{
		cur_vec[i] = center_vec[0];
		if( abs( a[j] ) < 0.00001 )
			cur_vec[j] = center_vec[j];
		else
			cur_vec[k] = center_vec[k]+a[k]*b[j]/a[j];
	}
	else
	{
		if( abs( a[j] ) < 0.00001 )
			cur_vec[k] = center_vec[k]+a[k]*b[i]/a[i];
		else
		{
			if( abs( b[i]/a[i] ) > abs( b[j]/a[j] ))
			{
				cur_vec[j] = center_vec[j]+a[j]*b[i]/a[i];
				cur_vec[k] = center_vec[k]+a[k]*b[i]/a[i];
			}
			else
			{
				cur_vec[i] = center_vec[i]+a[i]*b[j]/a[j];
				cur_vec[k] = center_vec[k]+a[k]*b[j]/a[j];
			}
		}
	}
	cur = Vertex( cur_vec[0], cur_vec[1], cur_vec[2] );
}

//
//	柺偺奼戝丒弅彫乮flag == ON 偺偲偒娭楢偡傞揰傕堏摦乯
//
function _Scale( rerationflag )
{
	var	org_pos[2], point[2], temp_point[2];
	var	i, j, k, key, ver;
	var	move_mat, mat;
	var	org_vec[2][3], temp_vec[3];
	var	pick;
	var	vers, temp, temp2, temp3, mode = OFF, draw_flag = ON;
	var	edge, edge1, edge2, edge_temp;
	var	prev_sel, sel1, sel2;
	var	change_ver[MAXVERTEX], change;

	if ( SelectPolygons() == 0 )
	{
		Warning();
		if( novice_flag == ON )
			Message( error_msg3 );
		return ;
	}

	PushMenu();
	Status_org = Status_title;
	ClearStatus();
	if( rerationflag == ON )
	{
		MenuPosition( Menu( " 摦嶌愝掕(&S)",
				"奼戝棪擖椡  ",		Input_scale,
				"------------",		SEPARATE_MENU,
				"奼戝弅彫拞巭	ESC",	MenuQuit
			), Menu_Title );
		Status_title[0] = "亂慖戰柺傪奼戝弅彫拞亃";
	}
	else
	{
		MenuPosition( Menu( " 摦嶌愝掕(&S)",
				"奼戝棪擖椡  ",		Input_scale,
				"------------",		SEPARATE_MENU,
				"奼戝弅彫拞巭	ESC",	MenuQuit
			), Menu_Title );
		Status_title[0] = "亂奼戝弅彫拞亃";
	}
	quit_flag = FALSE;

	prev_sel = Select();
	SelectBox( org_pos );
//	愨懳抣偺戝偒偄曽偺揧帤偑 1

	for( i = 0;i< 2; i++ )
		Position( org_pos[i], org_vec[i] );
	if ( ShiftStat() & 1 )
	{
		cur = Cursor();
		Position( cur, temp_vec );
		for( i = 0; i< 3; i++ )
		{
			if( abs( org_vec[0][i] - temp_vec[i] ) > abs( org_vec[1][i] - temp_vec[i] ))
			{
				org_vec[0][i] = temp_vec[i] - abs( org_vec[0][i] - temp_vec[i] );
				org_vec[1][i] = temp_vec[i] + abs( org_vec[0][i] - temp_vec[i] );
			}
			else
			{
				org_vec[0][i] = temp_vec[i] - abs( org_vec[1][i] - temp_vec[i] );
				org_vec[1][i] = temp_vec[i] + abs( org_vec[1][i] - temp_vec[i] );
			}
		}
		org_pos[0] = Vertex( org_vec[0][0], org_vec[0][1], org_vec[0][2] );
		org_pos[1] = Vertex( org_vec[1][0], org_vec[1][1], org_vec[1][2] );
		center = cur;
		Position( cur, temp_vec );
	}
	else if( ShiftStat() & 2 )
	{
		for( i = 0; i< 3; i++ )
		{
			if( abs( org_vec[0][i] ) > abs( org_vec[1][i] ) )
				temp_vec[i] = abs( org_vec[0][i] );
			else
				temp_vec[i] = abs( org_vec[1][i] );
		}
		org_pos[0] = Vertex( - temp_vec[0], - temp_vec[1], - temp_vec[2] );
		org_pos[1] = Vertex( temp_vec[0], temp_vec[1], temp_vec[2] );
		center = Vertex( 0, 0, 0 );
		for( i = 0; i< 3;i++ )
			temp_vec[i] = 0;
	}
	else
	{
		for(i = 0;i<3;i++)
			temp_vec[i] = (org_vec[0][i]+org_vec[1][i])/2;
		center = Vertex( temp_vec[0], temp_vec[1], temp_vec[2] );
	}
	move_mat = move( UNIT_MATRIX, - temp_vec[0], - temp_vec[1], - temp_vec[2] );
	cur = org_pos[1];
	Cursor( cur );
	pick = cur;
	mat = UNIT_MATRIX;
	get_pos = org_pos[1];
	ver = 1;
	element((Position(cur) - Position(center)), org_vec[0] );
	element((Position(get_pos) - Position(center)), org_vec[1] );
	for( i = 0; i< 3;i++ )
	{
		if( org_vec[1][i] == 0 )
			scal[i] = 1.0;
		else
			scal[i] = (0.0+org_vec[0][i]) / org_vec[1][i];
	}
	PrintScale();

	if( rerationflag == ON )
	{
		sel1 = Select();
		edge1 = EdgeSelect();
		sel2 = SelectAdjoint( sel1 );
		sel2 = sel2 & ( !sel1 );
		Select( sel2 );
		edge2 = EdgeSelect();
		edge2 = edge2 - edge1;
		edge  = Edge();
		for( i = 0; i< Edges(edge2 );i++ )
		{
			EdgeGetVertex( edge2, i, point );
			Select( prev_sel );
			SelectArea( TRUE, SELECT_AND | SELECT_SUB, point[0], point[0] );
			if( SelectPolygons() != 0 )
			{
				edge_temp = Edge( point[0], point[1] );
				EdgeGetVertex( edge_temp, 0, temp_point );
				if( temp_point[0] == point[0] )
					edge = edge + edge_temp;
				else
				{
					edge = edge + edge_temp;
					edge = edge + edge_temp;
				}
			}
			else
			{
				Select( prev_sel );
				SelectArea( TRUE, SELECT_AND | SELECT_SUB, point[1], point[1] );
				if( SelectPolygons() != 0 )
				{
					edge_temp = Edge( point[0], point[1] );
					EdgeGetVertex( edge_temp, 0, temp_point );
					if( temp_point[0] == point[1] )
						edge = edge + edge_temp;
					else
					{
						edge = edge + edge_temp;
						edge = edge + edge_temp;
					}
				}
			}
		}
		j = Edges( edge );
	}

	Select( prev_sel );
	edge_temp = EdgeSelect();
	k = Edges( edge_temp );
	input_flag = OFF;
	while( TRUE )
	{
		if( draw_flag == ON )
		{
			if( ver == 0 )
				DrawLine( center, cur );
			else
			{
				mat = move_mat * scale( UNIT_MATRIX, scal[0], scal[1], scal[2] ) / move_mat;
				if( mode == OFF )
				{
					DrawBox( org_pos[0], org_pos[1], mat );
					DrawLine( center, get_pos );
				}
				else
				{
					DrawLine( center, get_pos );
					for( i = 0; i< k; i++ )
					{
						EdgeGetVertex( edge_temp, i, temp_point );
						DrawLine( temp_point[0]*mat, temp_point[1]*mat );
					}
					if( rerationflag == ON )
					{
						for( i = 0; i< j; i++ )
						{
							EdgeGetVertex( edge, i, point );
							if( EdgeGetCount( edge, i ) == 1 )
								DrawLine( point[0]*mat, point[1] );
							else
								DrawLine( point[0], point[1]*mat );
						}
					}
				}
			}
		}
		key = WaitEvent();
		if( key )
			input_flag = ON;
		if ( ShiftStat() & 1 )
		{
			cur = Cursor();
			if( ver == 0 )
				TrimCursor( center, MouseWindow());
			else
				TrimCursorforScale(MouseWindow());
			Cursor( cur );
		}
		else
			cur = Cursor();
		key = KeyCode();
		if( input_flag | key == INPUT_KEY )
		{
			if( ver == 0 )
			{
				get_pos = cur;
				ver++;
			}
			else
				break;
		}
		else if( key == ESC | quit_flag == TRUE )
		{
			PopandClear( OVERWRITE );
			Select( prev_sel );
			return;
		}
		else if( key == BS )
		{
			if( ver == 0 )
			{
				PopandClear( OVERWRITE );
				Select( prev_sel );
				return;
			}
			else
				ver--;
		}
		else if( key == KEY_CLR )
			Input_scale();
		else if( key =='c' )
			mode = !mode;
		else
			temp = KeyProcess( key );
		if( ver == 1 )
		{
			element((Position(cur)-Position(center)), org_vec[0] );
			element((Position(get_pos)-Position(center)), org_vec[1] );
			for( i = 0; i< 3;i++ )
			{
				if( org_vec[1][i] == 0 )
					scal[i] = 1.0;
				else
					scal[i] = (0.0+org_vec[0][i]) / org_vec[1][i];
			}
			PrintScale();
		}
		if( ((key!=0 ) & (key != INPUT_KEY)) | ( pick != Cursor()) )
		{
			draw_flag = ON;
			Clear( OVERWRITE );
		}
		else
			draw_flag = OFF;
		pick = Cursor();
		input_flag = OFF;
	}
	Clear( OVERWRITE );
	element((Position(cur)-Position(center)), org_vec[0] );
	element((Position(get_pos) - Position(center)), org_vec[1] );
	for( i = 0; i< 3;i++ )
	{
		if( org_vec[1][i] == 0 )
			scal[i] = 1.0;
		else
			scal[i] = (0.0+org_vec[0][i]) / org_vec[1][i];
	}
	mat = move_mat * scale( UNIT_MATRIX, scal[0], scal[1], scal[2] ) / move_mat;
	if( rerationflag == ON )
	{
		Select(sel2 );
		k = SelectPolygons();
		for( i = 0; i< k; i++ )
		{
			PolyLoad( sel2, i );
			vers = PolyVertexs();
			change = 0;
			for( j = 0; j< vers; j++ )
			{
				temp = PolyGetVertex( j );
				Select( prev_sel );
				SelectArea( TRUE, SELECT_AND | SELECT_SUB, temp, temp );
				if( SelectPolygons() != 0 )
				{
					change_ver[change] = j;
					change++;
				}
			}
			PolyLoad( sel2, i );
			for( j = 0; j< change; j++ )
			{
				temp = PolyGetVertex( change_ver[j] );
				PolySetVertex( temp*mat, change_ver[j] );
			}
			PolySave();
		}
	}
	Select( prev_sel );
	PolyMove( mat );
	Update( CLEAR );
	UpdateObject();
	pers_rotation_flag = FALSE;
	PopMenu();
	Status_title = Status_org;
	DrawStatus();
}
